
import java.util.*;
import javax.crypto.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.*;
import javax.swing.JOptionPane;



public class Test {

  /** cipher object for password-based encryption */
  private Cipher encCipher = null;

  /** cipher object for password-based decryption */
  private Cipher decCipher = null;

  /** PBE parameter set */
  private PBEParameterSpec pbeParamSpec = null;

  /** SecretKey object */
  private SecretKey pbeKey = null;


  public Test() {
    try {
      long time = System.currentTimeMillis();

      // Salt
      byte[] salt = {
          (byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
          (byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
      };



      // Iteration count
      int count = 20;

      // Create PBE parameter set
      pbeParamSpec = new PBEParameterSpec(salt, count);

      // Prompt user for encryption password.
      // Collect user password as char array (using the
      // "readPasswd" method from above), and convert
      // it into a SecretKey object, using a PBE key
      // factory.
      PBEKeySpec pbeKeySpec = new PBEKeySpec(new char[]{'2','1','1','7','4'});
      SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
      pbeKey = keyFac.generateSecret(pbeKeySpec);

      // get cipher object for password-based encryption
      encCipher = Cipher.getInstance("PBEWithMD5AndDES");

      // get cipher object for password-based decryption
      decCipher = Cipher.getInstance("PBEWithMD5AndDES");

      System.out.println((System.currentTimeMillis()-time)+"ms");

      boolean ok = false;
      String s = "iuppiter";
//      String s = "iupp_4ws";
//      String s = "svil_4ws";
      while(!ok) {
        try {
          byte[] bb = encode(s);
          String t = decode(bb);
          System.out.println("ENCODE='"+s+"'");
          System.out.println("DECODE='" + t + "'");
          ok = true;
        }
        catch (Exception ex1) {
          s += " ";
        }
      }

    }
    catch (Throwable ex) {
      ex.printStackTrace();
    }
  }


  public static void main(String[] args) {
    Test test1 = new Test();
  }




  /**
   * Encrypt text.
   * @param text text to encrypt
   * @return encrypted text
   */
  public final byte[] encode(String text) throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException, InvalidAlgorithmParameterException {
    encCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
    byte[] cipherText = encCipher.doFinal(text.getBytes());
    return cipherText;
  }


  /**
   * Decrypt text.
   * @param text text to decrypt
   * @return decrypted text
   */
  public final String decode(byte[] text)  throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException, InvalidAlgorithmParameterException {
    // initialize cipher for decryption, without supplying
    // any parameters. Here, "myKey" is assumed to refer
    // to an already-generated key.
    decCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);
    byte[] cipherText = decCipher.doFinal(text);
    return new String(cipherText);
  }


}